package com.app.controller.exportExcel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.app.common.SystemConstant;
import com.app.model.*;
import com.app.service.EnjoyService;
import com.app.service.PersonService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 政治工作导出
 */
@RestController
@Controller
@RequestMapping("/api/excel")
public class PoliticalWorkController {
    @Autowired
    private EnjoyService enjoyService;
    @Autowired
    private PersonService personService;

    /**
     * 福利费导出
     *
     * @param response
     */
    @PostMapping("/enjoyExcel")
    public void enjoyExcel(@RequestBody JSONObject json, HttpServletResponse response) throws IOException {
        Integer personId = (Integer) json.get("personId");
        Map<String, Object> map = new HashMap<>();
        //获取列表
        List<EnjoyModel> list = enjoyService.getList(personId);
        for (int i = 0; i < list.size(); i++) {
            PersonModel personModel = personService.selectById(list.get(i).getPersonId());
            if (personModel != null) {
                list.get(i).setName(personModel.getName());
                list.get(i).setGenderText(personModel.getGender());
                list.get(i).setAdministrativePosition(personModel.getAdministrativePosition());
                list.get(i).setIdCard(personModel.getIdcard());
                list.get(i).setPositionDesc(personModel.getPositionDesc());
                list.get(i).setActualJob(personModel.getActualJob());
            }
            List<EnjoyFeeModel> separationList = list.get(i).getSeparationList();
            map.put("separationList", separationList);
            List<EnjoyFeeModel2> noEmployList = list.get(i).getNoEmployList();
            map.put("noEmployList", noEmployList);
            List<EnjoyFeeModel3> enjoyFeeModels = list.get(i).getEducationFeeList();
            map.put("enjoyFeeModels", enjoyFeeModels);
            List<EnjoyFeeModel1> provideList = list.get(i).getProvideList();
            map.put("provideList", provideList);
            List<EnjoyFeeModel4> supportAllowanceList = list.get(i).getSupportAllowanceList();
            map.put("supportAllowanceList", supportAllowanceList);
            List<EnjoyFeeModel5> comfortList = list.get(i).getComfortList();
            map.put("comfortList", comfortList);
            List<EnjoyFeeModel6> notArmyList = list.get(i).getNotArmyList();
            map.put("notArmyList", notArmyList);


        }
        map.put("list", list);
        TemplateExportParams params = new TemplateExportParams(SystemConstant.EnjoyFeeExportURL, true);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String fileName = "福利费表" + time + ".xlsx";
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
        exportExcel(params, map, fileName, response);
    }

    public static void exportExcel(TemplateExportParams params, Map<String, Object> map, String fileName, HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
            ServletOutputStream outStream = null;
            try {
                outStream = response.getOutputStream();
                workbook.write(outStream);

            } finally {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
